package com.yourname.myapplication

import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.yourname.myapplication.activity.LoginActivity
import com.yourname.myapplication.activity.RegisterActivity
import com.yourname.myapplication.activity.QuestionActivity
import com.yourname.myapplication.viewmodel.LoginViewModel
import com.yourname.myapplication.ui.theme.MyApplicationTheme
import android.content.Context
import androidx.compose.foundation.layout.*
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.yourname.myapplication.activity.StudyRecordActivity
import com.yourname.myapplication.database.AppDatabase

class MainActivity : ComponentActivity() {
    private val sharedPreferences by lazy {
        getSharedPreferences("user_prefs", Context.MODE_PRIVATE)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyApplicationTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    val loginViewModel: LoginViewModel = viewModel(
                        factory = viewModelFactory {
                            initializer {
                                LoginViewModel(AppDatabase.getInstance(applicationContext))
                            }
                        }
                    )
                    val isLoggedIn = sharedPreferences.getBoolean("is_logged_in", false)
                    MainContent(
                        modifier = Modifier.padding(innerPadding),
                        onRegisterClick = {
                            val intent = Intent(this, RegisterActivity::class.java)
                            startActivity(intent)
                        },
                        onLoginClick = {
                            val intent = Intent(this, LoginActivity::class.java)
                            startActivity(intent)
                        },
                        onStartPractice = { category ->
                            val intent = Intent(this, QuestionActivity::class.java)
                            intent.putExtra("category", category)
                            startActivity(intent)
                        },
                        onViewStudyRecord = {
                            val intent = Intent(this, StudyRecordActivity::class.java)
                            startActivity(intent)
                        },
                        isLoggedIn = isLoggedIn,
                        questionCategories = listOf("分类 1", "分类 2", "分类 3") // 模拟题目分类
                    )
                }
            }
        }
    }
}

@Composable
fun MainContent(
    modifier: Modifier = Modifier,
    onRegisterClick: () -> Unit,
    onLoginClick: () -> Unit,
    onStartPractice: (String) -> Unit,
    onViewStudyRecord: () -> Unit,
    isLoggedIn: Boolean,
    questionCategories: List<String>
) {
    Column(
        modifier = modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        if (isLoggedIn) {
            // 已登录，显示核心功能
            Text(text = "欢迎回来，开始刷题吧！", fontWeight = FontWeight.Bold)
            Spacer(modifier = Modifier.height(16.dp))
            Button(
                onClick = { onStartPractice(questionCategories[0]) },
                modifier = Modifier.padding(top = 16.dp)
            ) {
                Text(text = "开始刷题")
            }
            Spacer(modifier = Modifier.height(16.dp))
            Button(
                onClick = onViewStudyRecord,
                modifier = Modifier.padding(top = 16.dp)
            ) {
                Text(text = "查看学习记录")
            }
            Spacer(modifier = Modifier.height(16.dp))
            // 题目分类展示
            Column(
                modifier = Modifier.fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(text = "题目分类", fontWeight = FontWeight.Bold)
                Spacer(modifier = Modifier.height(8.dp))
                questionCategories.forEach { category ->
                    Button(
                        onClick = { onStartPractice(category) },
                        modifier = Modifier.padding(top = 8.dp)
                    ) {
                        Text(text = category)
                    }
                }
            }
        } else {
            // 未登录，显示登录和注册入口
            Text(text = "欢迎使用本应用")
            Button(
                onClick = onRegisterClick,
                modifier = Modifier.padding(top = 16.dp)
            ) {
                Text(text = "前往注册")
            }
            Button(
                onClick = onLoginClick,
                modifier = Modifier.padding(top = 16.dp)
            ) {
                Text(text = "前往登录")
            }
        }
    }
}
@Preview(showBackground = true)
@Composable
fun MainContentPreview() {
    MyApplicationTheme {
        MainContent(
            onRegisterClick = {},
            onLoginClick = {},
            onStartPractice = {},
            onViewStudyRecord = {},
            isLoggedIn = false,
            questionCategories = listOf("分类 1", "分类 2")
        )
    }
}